package com.nh.actionTerminal;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.nh.entity.GKLBookType;
import com.nh.entity.GKLClassType;
import com.nh.entity.ImageVO;

public class TmUpdataAction extends GKLTerminalBaseAction {

	public void updata() {
		Criteria criteria = this.createCriteria(GKLClassType.class);
		criteria.addOrder(Order.asc("typeOrder"));
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.registerJsonValueProcessor(Long.class, new LongValueProcessor());
		config.registerJsonValueProcessor(int.class, new LongValueProcessor());
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark", "createTime", "lastUpdataTime", "imagelistString","imageInfoList" });

		Criteria criteria2 = this.createCriteria(GKLBookType.class);
		criteria2.addOrder(Order.asc("typeOrder"));
		List<GKLBookType> infoList = criteria2.list();
		Set<String> imageList = new HashSet<String>();
		Set<String> detailImageList = new HashSet<String>();
		String temp = getProjectPath();
		for (GKLBookType info : infoList) {
			imageList.add(temp + info.getImageAdd());
			for (ImageVO imageVO : info.getImageInfoList()) {
				detailImageList.add(imageVO.getImageURl());
			}

		}

		TmVo vo = new TmVo();
		vo.setClassTypeList(criteria.list());
		vo.setInfoList(infoList);
		vo.setImageURLList(imageList);
		vo.setDeatilImageURLList(detailImageList);

		writResponse(JSONSerializer.toJSON(vo, config).toString());
	}
}

class LongValueProcessor implements JsonValueProcessor {

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {

		return value.toString();
	}
}